package net.zjitc.servlet;

import net.zjitc.domain.Product;
import net.zjitc.service.ProductService;
import net.zjitc.service.impl.ProductServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hasee on 2017/10/27.
 */
@WebServlet(name = "AddProductServlet",urlPatterns = "/addProductServlet")
public class AddProductServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        String pName = request.getParameter("pName");
        String pShopPrice =request.getParameter("pShopPrice");
        String pDesc = request.getParameter("pDesc");
        Product product =new Product();
        Map map = request.getParameterMap();
        try {
            BeanUtils.populate(product,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        ProductService service = new ProductServiceImpl();
        service.insert(product);
        response.sendRedirect("/web/productListServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
